#include "pwm.h"
void PWM_Init(void)
{
    /*打开TIM1时钟*/
    U32SetBit(RCC->APBENR2,11);

    /*CR1*/
    //设置中央对齐模式
    U32SetBitField(TIM1->CR1,5,6,1);//CMS1[1:0] = 0b01 中央对齐模式1
    //使能ARR预装载
    U32SetBit(TIM1->CR1,7);

    /*CR2*/
    //设置TRGO2
    U32SetBitField(TIM1->CR2,20,23,7);//MMS2[3:0] = 0b0111 OC4REFC->TRGO2->ADC

    /*CCMR1*/
    //设置定时器通道模式
    U32SetBitField(TIM1->CCMR1,0,1,0);//CC1S[1:0] = 0b00 通道模式为输出比较模式
    U32SetBitField(TIM1->CCMR1,8,9,0);//CC2S[1:0] = 0b00 通道模式为输出比较模式
    //设置输出比较模式
    U32SetBitField(TIM1->CCMR1,4,6,4);//OC1M[2:0] = 0b100 Force inactive level    
    U32SetBitField(TIM1->CCMR1,12,14,4);//OC2M[2:0] = 0b100 Force inactive level
    //使能CCR预装载
    U32SetBit(TIM1->CCMR1,3);    
    U32SetBit(TIM1->CCMR1,11);

    /*CCMR2*/
    //设置定时器通道模式
    U32SetBitField(TIM1->CCMR2,0,1,0);//CC3S[1:0] = 0b00 通道模式为输出比较模式
    U32SetBitField(TIM1->CCMR2,8,9,0);//CC4S[1:0] = 0b00 通道模式为输出比较模式
    //设置输出比较模式
    U32SetBitField(TIM1->CCMR2,4,6,4);//OC3M[2:0] = 0b100 Force inactive level    
    U32SetBitField(TIM1->CCMR2,12,14,6);//OC4M[2:0] = 0b110 PWM mode 1
    //使能CCR预装载
    U32SetBit(TIM1->CCMR2,3);    
    U32SetBit(TIM1->CCMR2,11);

    /*BDTR*/
    //使能刹车功能
    // U32SetBit(TIM1->BDTR,12);
    //使能主输出
    // U32SetBit(TIM1->BDTR,15);
    //使能自动输出
    // U32SetBit(TIM1->BDTR,14);
    // U32SetBitField(TIM1->BDTR,16,19,9);//设置刹车输入消抖，消抖时间1us BKF[3:0] = 0b1001 fSAMPLING=fDTS/8, N=8
    //                                 //fSAMPLING = 64MHZ/8/8 = 1MHZ
    TIM1->BDTR = 0x0009D000;//手册说BKE写入后会触发写保护，导致BKF写不进去，因此采用对寄存器一次性赋值的方式写
    /*AF1*/
    //使能刹车1输入
    U32SetBit(TIM1->AF1,0);

    /*CCER*/
    /*需求是上电后CH和互补路都输出低电平，等自举电容充完电再输出PWM,
      已知CH1、CH2、CH3强制inactive电平而由于互补所以CH1N、CH2N、CH3N强制active电平，
      因此互补路的active电平需要配置成低电平*/
    U32SetBit(TIM1->CCER,3);//CC1NP = 1 active low
    U32SetBit(TIM1->CCER,7);//CC2NP = 1 active low    
    U32SetBit(TIM1->CCER,11);//CC3NP = 1 active low
    //使能CH1,CH2,CH3,CH4和和CH1N CH2N CH3N
    U32SetBit(TIM1->CCER,0);//CH1 
    U32SetBit(TIM1->CCER,4);//CH2
    U32SetBit(TIM1->CCER,8);//CH3
    U32SetBit(TIM1->CCER,12);//CH4
    U32SetBit(TIM1->CCER,2);//CH1N  
    U32SetBit(TIM1->CCER,6);//CH2N 
    U32SetBit(TIM1->CCER,10);//CH3N 

    /*AF2*/
    //失能刹车2输入
    U32ClearBit(TIM1->AF2,0);

    /*ARR CCR PSC*/
    //PWM频率16K 64000000/(ARR+1)/2 = 16000   ARR = 1999
    TIM1->PSC = 0;
    TIM1->ARR = 2000 -1 ;
    TIM1->CCR1 = 0;
    TIM1->CCR2 = 0;
    TIM1->CCR3 = 0;
    TIM1->CCR4 = 2;

    /*使能刹车中断请求*/
    //U32SetBit(TIM1->DIER,7);//BIE = 1

    /*使能定时器计数*/
    U32SetBit(TIM1->CR1,0);
}

void PWM_OFF(void)
{
    //上管强制给低电平，下管也低电平
    U32SetBitField(TIM1->CCMR1,4,6,4);//OC1M[2:0] = 0b100 Force inactive level    
    U32SetBitField(TIM1->CCMR1,12,14,4);//OC2M[2:0] = 0b100 Force inactive level
    U32SetBitField(TIM1->CCMR2,4,6,4);//OC3M[2:0] = 0b100 Force inactive level
    U32SetBit(TIM1->CCER,3);//CC1NP = 1 active low
    U32SetBit(TIM1->CCER,7);//CC2NP = 1 active low    
    U32SetBit(TIM1->CCER,11);//CC3NP = 1 active low

    U32SetBit(TIM1->CCER,0);//CC1E = 1 Compare mode enabled
    U32SetBit(TIM1->CCER,2);//CC1NE = 1 Compare mode enabled    
    U32SetBit(TIM1->CCER,4);//CC2E = 1 Compare mode enabled
    U32SetBit(TIM1->CCER,6);//CC2NE = 1 Compare mode enabled
    U32SetBit(TIM1->CCER,8);//CC3E = 1 Compare mode enabled    
    U32SetBit(TIM1->CCER,10);//CC3NE = 1 Compare mode enabled

    TIM1->CCR1 = 0;
    TIM1->CCR2 = 0;
    TIM1->CCR3 = 0;
}

void PWM_Start(uint32_t CCR)
{
    U32SetBitField(TIM1->CCMR1,4,6,6);//OC1M[2:0] = 0b110 PWM mode 1    
    U32SetBitField(TIM1->CCMR1,12,14,6);//OC2M[2:0] = 0b110 PWM mode 1
    U32SetBitField(TIM1->CCMR2,4,6,6);//OC3M[2:0] = 0b110 PWM mode 1
    U32ClearBit(TIM1->CCER,3);//CC1NP = 0 active High
    U32ClearBit(TIM1->CCER,7);//CC2NP = 0 active High
    U32ClearBit(TIM1->CCER,11);//CC3NP = 0 active High
    
    TIM1->CCR1 = CCR;
    TIM1->CCR2 = CCR;
    TIM1->CCR3 = CCR;    
}

void PWM_CH1H_Ctrl(PwmState eState,uint32_t CCR)
{
  switch(eState)
  {
    case PWM_ON:/*上路开，下路active电平要和上路一致，利用互补性质防止短路*/
        U32SetBitField(TIM1->CCMR1,4,6,6);//OC1M[2:0] = 0b110  PWM mode 1
        U32ClearBit(TIM1->CCER,3);//CC1NP = 0 active High
        U32SetBit(TIM1->CCER,0);//CC1E = 1
	      U32SetBit(TIM1->CCER,2);//使能下管输出比较
        TIM1->CCR1 = CCR;
        break;
    case PWM_Off:
        U32ClearBit(TIM1->CCER,0);//禁用上路输出比较
        break;
    default:
      break;
  }
}

//下管恒定高电平，上管强制低电平
void PWM_CH1N_ON(void)
{
  //上管恒定inactive电平
  U32SetBitField(TIM1->CCMR1,4,6,0x4);//OC1M[3:0]:Force inactive level - OC1REF is forced low.
  //上管active电平为高，那么inactive电平为低
  U32ClearBit(TIM1->CCER,1);//CC1P:0: OC1 active high
  //由于互补的存在，下管恒定active电平，配置active电平为高
  U32ClearBit(TIM1->CCER,3);//CC1NP:1: OC1N active High.
  //使能上下管PWM输出
  U32SetBit(TIM1->CCER,0);//CC1E:1
  U32SetBit(TIM1->CCER,2);//CC1NE:1
}


void PWM_CH1N_Ctrl(PwmState eState,uint32_t CCR)
{
  switch(eState)
  {
    case PWM_ON:/*下管开，上管关*/
        U32SetBitField(TIM1->CCMR1,4,6,6);//OC1M[2:0] = 0b110  PWM mode 1
        U32ClearBit(TIM1->CCER,3);//CC1NP = 0 active High
        U32ClearBit(TIM1->CCER,0);//CC1E = 0 禁用上路输出比较，防止短路
        U32SetBit(TIM1->CCER,2);//使能下管输出比较
        TIM1->CCR1 = CCR;
        break;
    case PWM_Off:
        U32ClearBit(TIM1->CCER,2);//禁用下路输出比较
        break;
    default:
      break;
  } 
}

//关闭上下管
void PWM_CH1_OFF(void)
{
  U32ClearBit(TIM1->CCER,2);//CC1NE = 0 OFF
  U32ClearBit(TIM1->CCER,0);//CC1E = 0 OFF
  U32SetBitField(TIM1->CCMR1,4,6,0);//OC1M[2:0] = 0  Frozen
}

void PWM_CH2H_Ctrl(PwmState eState,uint32_t CCR)
{
  switch(eState)
  {
    case PWM_ON:/*上路开，下路active电平要和上路一致，利用互补性质防止短路*/
        U32SetBitField(TIM1->CCMR1,12,14,6);//OC2M[2:0] = 0b110  PWM mode 1
        U32ClearBit(TIM1->CCER,7);//CC2NP = 0 active High
        U32SetBit(TIM1->CCER,4);//CC2E = 1
        U32SetBit(TIM1->CCER,6);//使能下管输出比较	
        TIM1->CCR2 = CCR;
        break;
    case PWM_Off:
        U32ClearBit(TIM1->CCER,4);//禁用上路输出比较
        break;
    default:
      break;
  }
}

void PWM_CH2N_Ctrl(PwmState eState,uint32_t CCR)
{
  switch(eState)
  {
    case PWM_ON:/*下管开，上管关*/
        U32SetBitField(TIM1->CCMR1,12,14,6);//OC2M[2:0] = 0b110  PWM mode 1
        U32ClearBit(TIM1->CCER,7);//CC2NP = 0 active High
        U32ClearBit(TIM1->CCER,4);//CC2E = 0 禁用上路输出比较，防止短路
        U32SetBit(TIM1->CCER,6);//使能下管输出比较
        TIM1->CCR2 = CCR;
        break;
    case PWM_Off:
        U32ClearBit(TIM1->CCER,6);//禁用下路输出比较
        break;
    default:
      break;
  } 
}

//下管恒定高电平，上管强制低电平
void PWM_CH2N_ON(void)
{
  //上管恒定inactive电平
  U32SetBitField(TIM1->CCMR1,12,14,0x4);//OC2M[3:0]:Force inactive level - OC1REF is forced low.
  //上管active电平为高，那么inactive电平为低
  U32ClearBit(TIM1->CCER,5);//CC2P:0: OC1 active high
  //由于互补的存在，下管恒定active电平，配置active电平为高
  U32ClearBit(TIM1->CCER,7);//CC2NP:0: OC1N active high.
  //使能上下管PWM输出
  U32SetBit(TIM1->CCER,4);//CC2E:1
  U32SetBit(TIM1->CCER,6);//CC2NE:1
}

//关闭上下管
void PWM_CH2_OFF(void)
{
  U32ClearBit(TIM1->CCER,6);//CC2NE = 0 OFF
  U32ClearBit(TIM1->CCER,4);//CC2E = 0 OFF
  U32SetBitField(TIM1->CCMR1,12,14,0);//OC2M[2:0] = 0  Frozen
}

void PWM_CH3H_Ctrl(PwmState eState,uint32_t CCR)
{
  switch(eState)
  {
    case PWM_ON:/*上路开，下路active电平要和上路一致，利用互补性质防止短路*/
        U32SetBitField(TIM1->CCMR2,4,6,6);//OC3M[2:0] = 0b110  PWM mode 1
        U32ClearBit(TIM1->CCER,11);//CC3NP = 0 active High
        U32SetBit(TIM1->CCER,8);//CC3E = 1
	      U32SetBit(TIM1->CCER,10);//使能下路输出比较
        TIM1->CCR3 = CCR;
        break;
    case PWM_Off:
        U32ClearBit(TIM1->CCER,8);//禁用上路输出比较
        break;
    default:
      break;
  }
}

void PWM_CH3N_Ctrl(PwmState eState,uint32_t CCR)
{
  switch(eState)
  {
    case PWM_ON:/*下管开，上管关*/
        U32SetBitField(TIM1->CCMR2,4,6,6);//OC3M[2:0] = 0b110  PWM mode 1
        U32ClearBit(TIM1->CCER,11);//CC3NP = 0 active High
        U32ClearBit(TIM1->CCER,8);//CC3E = 0 禁用上路输出比较，防止短路
        U32SetBit(TIM1->CCER,10);//使能下管输出比较
        TIM1->CCR3 = CCR;
        break;
    case PWM_Off:
        U32ClearBit(TIM1->CCER,10);//禁用下路输出比较
        break;
    default:
      break;
  } 
}

//下管恒定高电平，上管强制低电平
void PWM_CH3N_ON(void)
{
  //上管恒定inactive电平
  U32SetBitField(TIM1->CCMR2,4,6,0x4);//OC3M[3:0]:Force inactive level - OC1REF is forced low.
  //上管active电平为高，那么inactive电平为低
  U32ClearBit(TIM1->CCER,9);//CC3P:0: OC1 active high
  //由于互补的存在，下管恒定active电平，配置active电平为高
  U32ClearBit(TIM1->CCER,11);//CC3NP:1: OC1N active high.
  //使能上下管PWM输出
  U32SetBit(TIM1->CCER,8);//CC3E:1
  U32SetBit(TIM1->CCER,10);//CC3NE:1  

}

//关闭上下管
void PWM_CH3_OFF(void)
{
  U32ClearBit(TIM1->CCER,10);//CC3NE = 0 OFF
  U32ClearBit(TIM1->CCER,8);//CC3E = 0 OFF
  U32SetBitField(TIM1->CCMR2,4,6,0);//OC3M[2:0] = 0  Frozen
}

uint16_t PWMSetDutyCycle(uint8_t DutyCycle)
{
  /*DutyCycle/100 = TIM1->CNT / 2000; TIM1->CNT = DutyCycle*20*/
  return (TIM1->CNT = DutyCycle*20);
}
